package com.chixing.controller;

import com.chixing.commons.TokenUtil;
import com.chixing.pojo.LoginPerson;
import com.chixing.pojo.Myorders;
import com.chixing.pojo.TemplateKill;
import com.chixing.service.IMyordersService;
import com.chixing.service.IResumeTemplateService;
import com.chixing.service.ITemplateKillService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("templateKill")
public class TemplateKillController {

    @Autowired
    ITemplateKillService templateKillService;

    @Autowired
    IResumeTemplateService resumeTemplateService;

    @Autowired
    IMyordersService myordersService;

    @Autowired
    RabbitTemplate template;

    @GetMapping("/getAllTemplate")
    @ResponseBody
    public List<TemplateKill> getAllProduct() {
        List<TemplateKill> templateKillList = templateKillService.getAllTemplate();
        return templateKillList;
    }

    @GetMapping ("/{templateKillId}/{token}")
    public ModelAndView initMyOrder(@PathVariable("templateKillId") Integer templateId,
                                    @PathVariable("token") String token){
        //秒杀模板
        //先减库存
        String templateKillStr = getTemplateKillIdAndGenerateOrder(templateId);
        if(templateKillStr.equals("ok")){
            System.out.println("hello world");
            //减库存成功，生成订单，入队列
            TemplateKill templateKill = templateKillService.selectById(templateId);
            Myorders myOrder = new Myorders();
            myOrder.setTemplateId(templateId);
            LoginPerson loginPerson = TokenUtil.parseToken(token);
            myOrder.setPersonId(loginPerson.getPersonId());
            myOrder.setOrderCreateTime(LocalDate.now());
            myOrder.setOrderPrice(templateKill.getTemplateKillSalary());
            UUID uuid = UUID.randomUUID();
            String uuid1 = uuid.toString().replace("-", "");
            myOrder.setOrderNumber(uuid1);
            myOrder.setOrderState(0);//订单状态0未支付，1已支付
            Boolean aBoolean = myordersService.insertMyOrder(myOrder);
            Myorders myorders = myordersService.selectByOrderNumber(uuid1);
            ModelAndView modelAndView = new ModelAndView();
            if (aBoolean) {
                modelAndView.addObject("myOrdersList", myorders);
                modelAndView.addObject("template", templateKill);
                modelAndView.setViewName("public/resume/resume-kill-orders");
                template.convertAndSend("orderDelayExchange", "orderDelayKey", uuid1);
            } else {
                modelAndView.addObject("myOrdersList", "生成订单失败");
                modelAndView.setViewName("public/resume/resume-kill-orders");
            }
            return modelAndView;
        }else {
            //减库存不成功，返回库存不足
            ModelAndView modelAndView = new ModelAndView("public/resume/resume-failed");
            modelAndView.addObject("result", "宝贝，库存不足");
            return modelAndView;
        }

    }


    @GetMapping("skill/{templateKillId}")
    @ResponseBody
    public String getTemplateKillIdAndGenerateOrder(@PathVariable("templateKillId") Integer templateKillId) {
        //减去redis中库存
        TemplateKill templateKill = templateKillService.deleteInventory(templateKillId);
        //发送消息到消息队列，减DB库存
        templateKillService.sendMsg2deleteInventoryNumFromDB(templateKillId);
        if(templateKill!=null){
            return "ok";
        }else {
            return "已被抢光";
        }
    }
}
